package com.dotflat.training.service;

import com.alibaba.fastjson.JSON;
import com.dotflat.training.po.GoodsListPO;
import com.dotflat.training.po.LiveListPO;
import com.dotflat.training.po.ResTagPO;
import com.dotflat.util.excelUtil.StringUtils;
import com.google.common.collect.ImmutableMap;
import com.matt.foundation.utils.DateUtil;
import com.tuo51.components.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class XETApiService {

    @Value("${xiaoetong.appId}")
    private String appId;
//    @Autowired
//    private XETTokenService tokenService;

    private final
    String XET_API_LIVE_LIST_AP = "https://api.xiaoe-tech.com/xe.alive.list.get/1.0.0";

    private final
    String XET_API_GOODS_LIST_AP = "https://api.xiaoe-tech.com/xe.goods.list.get/4.0.0";

    private final
    String XET_API_TAG_LIST_AP = "https://api.xiaoe-tech.com/xe.resource_tags.list/1.0.0";

    private final
    String XET_API_TAG_RESOURCE_LIST_AP = "https://api.xiaoe-tech.com/xe.resource_tags.resource/1.0.0";


    private final int SUCCESS = 0;//	系统繁忙
    private final int SYSTEM_BUSY = 1001;//	系统繁忙
    private final int UNEXPECTED = 1002;//	业务接口返回值格式错误
    private final int NEED_APP_ID = 2001;//	缺失 app_id 参数
    private final int INVALID_APP_ID = 2002;//	错误 app_id 参数
    private final int NEED_SECRET_KEY = 2003;//	缺失 secret_key 参数
    private final int INVALID_SECRET_KEY = 2004;//	secret_key 错误，或者 app_id 无效。请开发者认真比对 app_id,secret_key 的正确性
    private final int NEED_GRANT_TYPE = 2005;//	缺失 grant_type 参数
    private final int NEED_GRANT_TYPE2 = 2006;//	缺失 grant_type 参数
    private final int NEED_ACCESS_TOKEN = 2007;//	缺失 凭证access_token
    private final int INVALID_ACCESS_TOKEN = 2008;//	access_token 过期 或者不存在
    private final int NEED_USER_ID = 2009;//	缺失 userid 参数
    private final int INVALID_USER_ID = 2010;//	user_id 无效
    private final int HAVE_NO_PERMISSION = 2017;//	没有请求本接口的权限，请申请对应接口权限集
    private final int INVALID_CLIENT_ID = 2020;//	无效的client_id
    private final int REQUEST_IP_NOT_ALLOWED = 2051;//	IP 没有添加白名单，需用用户配置服务器白名单ip
    private final int REQUEST_API_WRONG = 2052;//	没有提供该API，用户请求的url错误，或者改url没有在系统中注册
    private final int REQUEST_RATE_LIMITED = 2053;//	API的调用次数达到上线
    private final int REQUEST_INSTANT_RATE_LIMITED = 2054;//	API的请求短时间突增
    private final int REQUEST_METHOD_WRONG = 2055;//	API请求方式错误

    boolean b = true;
    @Autowired
    private RedisService redisService;

//    private Object xetRequest(String apURL, Map params){
//        String responseJson = null;
//        try {
//            responseJson = HttpClientUtil.sendPost(apURL, params);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        JSONObject json = JSON.parseObject(responseJson);
//
////        "code": 0,
////        "msg": 'success',
////        "data": { }
//        Object data = json.get("data");
//        Integer code = json.getInteger("code");
//        String msg = json.getString("msg");
//
//        if (code == INVALID_ACCESS_TOKEN){
//            tokenService.clearAccessToken();
//            //重新获取一次accessToken 并替换掉
//            if (b){
//                b=false;
//                String key = "xet_access_token_" + appId;
//                tokenService.updateAccessToken();
//                String accessToken =  redisService.getValue(key);
//                params.put("access_token",accessToken);
//                return xetRequest(apURL,params);
//            }
//
//            return data;
//        }
//
//        if (code != SUCCESS){
//           return null;
//        }
//
//        return data;
//    }

    public Map getLiveList(int pageNum, int pageSize, String keyword) {
        String accessToken = ""; // tokenService.accessToken();

        Map params = null;
        if (StringUtils.isNotEmpty(keyword)){
            params = ImmutableMap.of(
                    "access_token", accessToken,
                    "page", pageNum,
                    "page_size", pageSize,
                    "search_content",keyword);
        }else{
            params = ImmutableMap.of(
                    "access_token", accessToken,
                    "page", pageNum,
                    "page_size", pageSize);
        }

//        Map data = (Map) xetRequest(XET_API_LIVE_LIST_AP, params);
//        if (null == data){
//            return null;
//        }
        final String DEMO_STR="[\n" +
                "      {\n" +
                "        \"app_id\": \"app4h9oRFe93561\",\n" +
                "        \"id\": \"l_676cf777e4b0694c951bb5da\",\n" +
                "        \"room_id\": \"XET#9e4c632460000e2\",\n" +
                "        \"title\": \"金职托育通\",\n" +
                "        \"img_url\": \"https://wechatapppro-1252524126.file.myqcloud.com/app4h9oRFe93561/image/b_u_cr826k9nu5u9lu6harig/m5514xge0kj8.jpeg\",\n" +
                "        \"page_url\": \"https://app4h9oRFe93561.h5.xiaoeknow.com/v2/course/alive/l_676cf777e4b0694c951bb5da?app_id=app4h9oRFe93561&pro_id=&type=2\",\n" +
                "        \"img_url_compressed\": \"http://wechatapppro-1252524126.file.myqcloud.com/app4h9oRFe93561/image/compress/393225203m5514xge0kj8.jpeg\",\n" +
                "        \"comment_count\": 0,\n" +
                "        \"is_takegoods\": 0,\n" +
                "        \"takegoods\": \"0\",\n" +
                "        \"payment_type\": 1,\n" +
                "        \"is_public\": 1,\n" +
                "        \"is_stop_sell\": 0,\n" +
                "        \"is_transcode\": 1,\n" +
                "        \"piece_price\": 0,\n" +
                "        \"line_price\": 0,\n" +
                "        \"have_password\": 0,\n" +
                "        \"alive_type\": 2,\n" +
                "        \"purchase_count\": 0,\n" +
                "        \"reward_sum\": 0,\n" +
                "        \"is_ban\": 0,\n" +
                "        \"on_shelf\": 0,\n" +
                "        \"recycle_bin_state\": 0,\n" +
                "        \"push_state\": 2,\n" +
                "        \"state\": 0,\n" +
                "        \"start_at\": \"2024-12-26 14:28:07\",\n" +
                "        \"zb_start_at\": \"2024-12-26 15:00:00\",\n" +
                "        \"zb_stop_at\": \"2029-11-29 16:00:00\",\n" +
                "        \"manual_stop_at\": \"\",\n" +
                "        \"source_shop_name\": \"\",\n" +
                "        \"alive_video_url\": \"\",\n" +
                "        \"material_state\": 0,\n" +
                "        \"video_length\": 0,\n" +
                "        \"alive_state\": 1,\n" +
                "        \"alive_mode\": 0,\n" +
                "        \"create_mode\": 0,\n" +
                "        \"is_round_table_on\": 0,\n" +
                "        \"query_package_list\": null,\n" +
                "        \"course_expire\": {\n" +
                "          \"period_type\": 0,\n" +
                "          \"period_value\": \"\",\n" +
                "          \"is_allow_repeat_purchase\": 0\n" +
                "        },\n" +
                "        \"is_team_mode\": 0\n" +
                "      }\n" +
                "    ]";

        List<Map> mapList = null; //(List<Map>) data.get("list");

//        final String DEMO_STR = "[{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_673d2976e4b0694c9502e13e\",\"room_id\":\"XET#2dac45a32001e0697\",\"title\":\"2024年武乡培训\",\"img_url\":\"https://commonresource-1252524126.cdn.xiaoeknow.com/image/l6nfw9120t1u.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_673d2976e4b0694c9502e13e?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"https://commonresource-1252524126.cdn.xiaoeknow.com/image/l6nfw9120t1u.png\",\"comment_count\":113,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":0,\"state\":0,\"start_at\":\"2024-11-20 08:12:38\",\"zb_start_at\":\"2024-11-20 08:00:00\",\"zb_stop_at\":\"2024-11-20 19:00:00\",\"manual_stop_at\":\"\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":2,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"\",\"is_allow_repeat_purchase\":0}},{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_673bdb48e4b0694c3c69de24\",\"room_id\":\"XET#2da41d05800787b33\",\"title\":\"2024年武乡县托育从业人员职业技能提升培训班\",\"img_url\":\"https://commonresource-1252524126.cdn.xiaoeknow.com/image/l6nfw9120t1u.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_673bdb48e4b0694c3c69de24?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"https://commonresource-1252524126.cdn.xiaoeknow.com/image/l6nfw9120t1u.png\",\"comment_count\":294,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":0,\"state\":0,\"start_at\":\"2024-11-19 08:26:48\",\"zb_start_at\":\"2024-11-19 08:00:00\",\"zb_stop_at\":\"2024-11-19 19:00:00\",\"manual_stop_at\":\"\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":2,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"\",\"is_allow_repeat_purchase\":0}},{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_673a87c7e4b0694c3c69247c\",\"room_id\":\"XET#2d9bd31dc008b3855\",\"title\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v3/course/alive/l_673a8\",\"img_url\":\"https://commonresource-1252524126.cdn.xiaoeknow.com/image/l6nfw9120t1u.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_673a87c7e4b0694c3c69247c?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"https://commonresource-1252524126.cdn.xiaoeknow.com/image/l6nfw9120t1u.png\",\"comment_count\":301,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":0,\"state\":0,\"start_at\":\"2024-11-18 08:18:15\",\"zb_start_at\":\"2024-11-18 08:00:00\",\"zb_stop_at\":\"2024-11-18 19:00:00\",\"manual_stop_at\":\"\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":2,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"\",\"is_allow_repeat_purchase\":0}},{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_673833e0e4b023c058a57b10\",\"room_id\":\"XET#2d8d46bc900787b82\",\"title\":\"直播测试 1\",\"img_url\":\"https://commonresource-1252524126.cdn.xiaoeknow.com/image/l6nfw9120t1u.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_673833e0e4b023c058a57b10?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"https://commonresource-1252524126.cdn.xiaoeknow.com/image/l6nfw9120t1u.png\",\"comment_count\":312,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":0,\"state\":0,\"start_at\":\"2024-11-16 13:55:44\",\"zb_start_at\":\"2024-11-18 15:00:00\",\"zb_stop_at\":\"2024-11-26 16:00:00\",\"manual_stop_at\":\"\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":1,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"\",\"is_allow_repeat_purchase\":0}},{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_6401a18be4b06159f74479ce\",\"room_id\":\"XET#F4a042557q\",\"title\":\"第四讲 幼儿情绪和情感的发展 3\",\"img_url\":\"https://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/b_u_63ad07edea7e5_EHu4uII7/le7ze04z0ksl.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_6401a18be4b06159f74479ce?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"http://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/compress/301225539le7ze04z0ksl.png\",\"comment_count\":0,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":2,\"state\":0,\"start_at\":\"2023-03-03 15:28:10\",\"zb_start_at\":\"2023-03-03 20:00:00\",\"zb_stop_at\":\"2023-03-03 22:00:00\",\"manual_stop_at\":\"\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":2,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"\",\"is_allow_repeat_purchase\":0}},{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_63f85e1ee4b02685a448cf03\",\"room_id\":\"XET#Mdeec80c2t\",\"title\":\"第四讲 幼儿情绪和情感的发展 2\",\"img_url\":\"https://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/b_u_63ad07edea7e5_EHu4uII7/le7ze04z0ksl.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_63f85e1ee4b02685a448cf03?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"http://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/compress/400225836le7ze04z0ksl.png\",\"comment_count\":23,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":0,\"state\":0,\"start_at\":\"2023-02-24 14:50:06\",\"zb_start_at\":\"2023-02-24 20:00:00\",\"zb_stop_at\":\"2023-02-24 22:00:00\",\"manual_stop_at\":\"2023-02-24 21:06:42\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":2,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"\",\"is_allow_repeat_purchase\":0}},{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_63eef729e4b06159f73c9a42\",\"room_id\":\"XET#Rbbe6dd9aw\",\"title\":\"第四讲 幼儿情绪和情感的发展\",\"img_url\":\"https://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/b_u_63ad07edea7e5_EHu4uII7/le7ze04z0ksl.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_63eef729e4b06159f73c9a42?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"http://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/compress/301225539le7ze04z0ksl.png\",\"comment_count\":71,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":0,\"state\":0,\"start_at\":\"2023-02-17 11:40:24\",\"zb_start_at\":\"2023-02-17 20:00:00\",\"zb_stop_at\":\"2023-02-17 22:00:00\",\"manual_stop_at\":\"2023-02-17 21:06:35\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":2,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"0\",\"is_allow_repeat_purchase\":0}},{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_63b535c8e4b0fc5d1220aa51\",\"room_id\":\"XET#W84652ad6s\",\"title\":\"第三讲 学前儿童个性的发展\\u000b（气质、性格和能力）\",\"img_url\":\"https://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/b_u_63ad07edea7e5_EHu4uII7/lchdxc520xgh.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_63b535c8e4b0fc5d1220aa51?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"http://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/compress/301225639lchdxc520xgh.png\",\"comment_count\":0,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":2,\"state\":0,\"start_at\":\"2023-01-04 16:16:07\",\"zb_start_at\":\"2022-12-02 20:00:00\",\"zb_stop_at\":\"2022-12-02 22:00:00\",\"manual_stop_at\":\"\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":2,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"\",\"is_allow_repeat_purchase\":0}},{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_63b53572e4b02685a4323e44\",\"room_id\":\"XET#X7476a74et\",\"title\":\"11.25专家直播课（麻教授）\",\"img_url\":\"https://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/b_u_63ad07edea7e5_EHu4uII7/lchdvinp001l.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_63b53572e4b02685a4323e44?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"http://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/compress/300225673lchdvinp001l.png\",\"comment_count\":0,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":2,\"state\":0,\"start_at\":\"2023-01-04 16:14:41\",\"zb_start_at\":\"2022-11-25 20:00:00\",\"zb_stop_at\":\"2022-11-25 22:00:00\",\"manual_stop_at\":\"\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":2,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"\",\"is_allow_repeat_purchase\":0}},{\"app_id\":\"appqdvvl4xt7143\",\"id\":\"l_63b53518e4b06159f729f274\",\"room_id\":\"XET#Oc7af0ae6y\",\"title\":\"婴幼儿社会性发展（麻教授）\",\"img_url\":\"https://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/b_u_63ad07edea7e5_EHu4uII7/lchdtm0d06tm.png\",\"page_url\":\"https://appqdvvl4xt7143.h5.xiaoeknow.com/v2/course/alive/l_63b53518e4b06159f729f274?app_id=appqdvvl4xt7143&pro_id=&type=2\",\"img_url_compressed\":\"http://wechatapppro-1252524126.file.myqcloud.com/appqdvvl4xt7143/image/compress/302225881lchdtm0d06tm.png\",\"comment_count\":0,\"is_takegoods\":0,\"takegoods\":\"0\",\"payment_type\":1,\"is_public\":1,\"is_stop_sell\":0,\"is_transcode\":1,\"piece_price\":0,\"line_price\":0,\"have_password\":0,\"alive_type\":2,\"purchase_count\":0,\"reward_sum\":0,\"is_ban\":0,\"on_shelf\":0,\"recycle_bin_state\":0,\"push_state\":2,\"state\":0,\"start_at\":\"2023-01-04 16:13:12\",\"zb_start_at\":\"2022-11-18 20:00:00\",\"zb_stop_at\":\"2022-11-18 22:00:00\",\"manual_stop_at\":\"\",\"source_shop_name\":\"\",\"alive_video_url\":\"\",\"material_state\":0,\"video_length\":0,\"alive_state\":2,\"alive_mode\":0,\"create_mode\":0,\"is_round_table_on\":0,\"query_package_list\":null,\"course_expire\":{\"period_type\":0,\"period_value\":\"\",\"is_allow_repeat_purchase\":0}}]";
        mapList = JSON.parseArray(DEMO_STR, Map.class);

        List<LiveListPO> liveList = mapList.stream().map(o -> {
            LiveListPO liveListPO = new LiveListPO();
            liveListPO.setCover(String.valueOf(o.get("img_url")));
            liveListPO.setLiveDetailUrl(String.valueOf(o.get("page_url")));
            liveListPO.setAlias(liveListPO.getLiveDetailUrl());
            liveListPO.setTitle(String.valueOf(o.get("title")));
            liveListPO.setCreatedAt(DateUtil.parseToDateMilis(String.valueOf(o.get("zb_start_at"))).getTime());

//            YZ： 直播课程状态。0：删除1:未开始2：进行中3：已结束
//            XET: -1全部；0未开始；1直播中；2已结束
            int s = Integer.parseInt("" + o.get("alive_state"));
            liveListPO.setLiveStatus(s+1);  //热修复
            List<Map> guestList = (List<Map>) o.get("guest_list");

            if (guestList != null && guestList.size()>0){
                String letrurer = (String) guestList.get(0).get("user_name");
                liveListPO.setLecturer(letrurer);
            }

            return liveListPO;
        }).collect(Collectors.toList());

        ImmutableMap ret = ImmutableMap.of("total", mapList.size(), "content", liveList);

        return ret;
    }

    public Map getGoodsList(int pageNum, int pageSize, String keyword) {
        String accessToken = ""; // tokenService.accessToken();

        //has_distribute	否	int	是否参与推广0-未参与 1-参与 不传默认返回所有数据
        //sale_status	否	int	上架状态1上架 0下架 2待上架 3-上架+待上架 不传默认返回所有数据
        //is_return_deleted	否	int	是否删除 0正常 1已删除 不传默认返回所有数据
        //is_return_forbid	否	int	商品是否被封禁：0 = 否，1 = 是 不传默认返回所有数据
        //is_return_stop_sell	否	int	是否停售：0否 1是 不传默认返回所有数据
        //is_return_sell_type	否	int	售卖方式：1 = 独立售卖，2 = 关联售卖（商品放入专栏/会员/训练营中售卖） 不传默认返回所有数据
        //is_return_display	否	int	是否显示：0否(隐藏状态) 1是(显示状态) 不传默认返回所有数据
        //is_return_sell_mode	否	array	售卖类型（1-自营 2-内容市场 3-课程商品) 不传默认返回所有数据
        //is_return_zero_price	否	int	是否返回0元商品：0否 1是 不传默认返回所有价格包括0元的商品
        //is_return_password	否	int	是否加密（0-不加密 1--加密） 不传默认返回所有商品
        //is_return_public	否	int	是否公开售卖(0-不公开 1-公开) 不传默认返回所有商品

        //
        //resource_type	否	int	资源类型 ：1-图文，2-音频，3-视频，4-直播，5-会员，
        // 6-专栏，7-圈子，8-大专栏，9-活动管理，16-付费打卡，20-电子书，21-实物商品，
        // 23-超级会员，25-训练营营期，29-线下课，31-小班课，34-练习，35-班课，37-大班课，
        // 41-有价优惠券，42-课时包，45-AI互动课，46-付费问答，47-付费问答-答主，50-训练营pro

        //请求参数
//        Map params = ImmutableMap.of(
//                "access_token", accessToken,
//                "page", pageNum,
//                "page_size", pageSize,
//                "goods_name",keyword);
//
//        //资源类型
//        params.put("resource_type", 6);
//
//        //默认查询参数
//        params.put("sale_status", 1);
//        params.put("is_return_deleted", 0);
//        params.put("is_return_forbid", 0);
//        params.put("is_return_stop_sell", 0);
//        params.put("is_return_display", 0);

//        Map data = (Map) xetRequest(XET_API_GOODS_LIST_AP, params);

        List<Map> mapList = null; //(List<Map>) data.get("list");

      final  String DEMO_STR="[\n" +
              "    {\n" +
              "      \"resource_id\": \"v_676cf581e4b0694c951bb354\",\n" +
              "      \"resource_type\": 3,\n" +
              "      \"title\": \"金职托育通\",\n" +
              "      \"img_url\": \"https://wechatapppro-1252524126.cdn.xiaoeknow.com/app4h9oRFe93561/image/b_u_ctmccgh53tt0jfcfad0g/dssgbnm54xkj87.jpg\",\n" +
              "      \"img_url_compressed\": \"https://wechatapppro-1252524126.file.myqcloud.com/app4h9oRFe93561/image/compress/414320797dssgbnm54xkj87.jpg\",\n" +
              "      \"is_transcode\": 1,\n" +
              "      \"view_count\": \"0\",\n" +
              "      \"auth_type\": 0,\n" +
              "      \"price\": 0,\n" +
              "      \"line_price\": 0,\n" +
              "      \"is_free\": 1,\n" +
              "      \"is_public\": 1,\n" +
              "      \"is_password\": 0,\n" +
              "      \"is_stop_sell\": 0,\n" +
              "      \"is_display\": 1,\n" +
              "      \"sale_at\": \"2024-12-26 14:19:45\",\n" +
              "      \"sale_status\": 1,\n" +
              "      \"sell_type\": 1,\n" +
              "      \"period\": {\n" +
              "        \"period_type\": 0,\n" +
              "        \"period_value\": \"-1\"\n" +
              "      },\n" +
              "      \"goods_type\": 1,\n" +
              "      \"is_ban\": 0,\n" +
              "      \"is_force_unshelve\": 0,\n" +
              "      \"position\": 1,\n" +
              "      \"is_join_market_act\": false,\n" +
              "      \"protect_status\": false,\n" +
              "      \"h5_url\": \"https://app4h9oRFe93561.h5.xiaoeknow.com/p/course/video/v_676cf581e4b0694c951bb354\"\n" +
              "    }\n" +
              "  ]";

        mapList = JSON.parseArray(DEMO_STR, Map.class);

        List<GoodsListPO> liveList = mapList.stream().map(o -> {
//            id	int	id	无
//            resource_id	string	资源id（唯一值）	无
//            spu_id	string	统一商品id	无
//            spu_type	string	统一商品类型	无
//            goods_category_id	string	商品分类id	无
//            goods_name	string	商品名称	无
//            goods_img	array	商品封面图（默认封面图）	无
//            custom_cover	string	主图视频自定义封面	无
//            sell_type	int	付费类型	付费类型：1 = 独立售卖，2 = 关联售卖
//            price_low	int	商品最低价（取自sku中最低价值）	单位：分
//            price_high	int	商品高价（取自最低价所在sku对应划线价）	单位：分
//            price_line	int	划线价(取值price_low所在sku划线价)	单位：分
//            visit_num	int	访问量	无
//            goods_tag	string	商品标签	无
//            goods_tag_is_show	int	商品标签是否展示	0:不展示;1:展示
//            sale_status	int	上架状态	0下架 1上架 2待上架
//            is_timing_sale	int	是否定时上架	1是 0否
//            timing_sale	string	定时上架时间	无
//            sale_at	string	上架的时间	无
//            has_distribute	int	是否参与推广分销	0 = 否，1 = 是
//            video_img_url	string	主图视频封面url	无
//            is_goods_package	int	是否带货	0 否 1 是
//            is_display	int	是否显示	0否(隐藏状态) 1是(显示状态)
//            is_stop_sell	int	是否停售	0否 1是
//            is_forbid	int	商品是否被封禁	0 = 否，1 = 是
//            is_ignore	int	商品是否被忽略	0 = 否，1 = 是
//            limit_purchase	int	限购数量	无
//            stock_deduct_mode	int	扣库存方式	0付款减库存 1拍下减库存
//            appraise_num	int	评价数	无
//            show_stock	int	是否展示库存	0-不展示 1-展示
//            is_best	int	是否精品	0否 1是
//            is_hot	int	是否热销产品	0否 1是
//            is_new	int	是否新品	0否 1是
//            is_recom	int	是否推荐	0否 1是
//            distribution_pattern	int	配送方式	无
//            freight	int	运费（单位：分）	无
//            audit_reason	string	审核原因	无
//            audit_time	string	审核时间	无
//            audit_user_id	string	审核人user_id	无
//            spu_extend	string	业务侧扩展字段	无
//            parent_spu_id	string	父级的spu_id	无
//            parent_app_id	string	父级的app_id	无
//            is_uniform_freight	int	是否是统一运费	1:统一运费 2:运费模板
//            freight_template_id	int	运费模板ID	无
//            img_url_compressed	string	压缩后的列表配图url	无
//            is_deleted	int	是否删除	0正常 1已删除
//            created_at	string	创建时间	无
//            updated_at	string	更新时间	无
//            attr	array	属性信息	无
//            extend	array	扩展	无
//            extend[0].sell_num	int	售卖数量	无
//            total	int	总数	无
            GoodsListPO po = new GoodsListPO();
//            po.setNum();
            po.setSellStatus(Integer.parseInt("" + o.get("sale_status")));
            po.setImageUrl("" + o.get("img_url_compressed"));

//            po.setItemId(Integer.parseInt("" + o.get("id")));
            po.setTitle("" + o.get("title"));
            po.setType(1); //商品类型枚举值，1：专栏； 2：内容； 4：直播 ；10：课程
            po.setMediaType(3); //内容的类型，1:图文内容2:音频内容3:视频内容

            String resId = "" + o.get("resource_id");
            int type = Integer.parseInt("" + o.get("resource_type"));
//            po.setAlias(genAlias(type, resId));
            po.setAlias(""+o.get("h5_url"));
            po.setUrl(po.getAlias());

            po.setSellerType(Integer.parseInt("" + o.get("sell_type")));

//            po.setCreatedTime(null);
//            po.setAuthor(null);

            return po;
        }).collect(Collectors.toList());

        ImmutableMap ret = ImmutableMap.of("total", mapList.size(), "content", liveList);

        return ret;
    }



    public List<ResTagPO> getResTagList(int pageNum, int pageSize) {
        String accessToken = ""; // tokenService.accessToken();

        //access_token	是	string	专属token
        //fields	否	string	要获取的字段名称，不传默认返所有，以’,’分割，如id,name
        //page	是	int	当前页数，默认为1
        //page_size	是	int	每页数据量，默认为10

        //请求参数
        Map params = ImmutableMap.of(
                "access_token", accessToken,
                "page", pageNum,
                "page_size", pageSize);


//        Map data = (Map) xetRequest(XET_API_TAG_LIST_AP, params);

        List<Map> mapList = null;// (List<Map>) data.get("list");

        final String DEMO_STR = "[\n" +
                "            {\n" +
                "                \"id\": 13063386,\n" +
                "                \"name\": \"全部课程\",\n" +
                "                \"alias\": \"全部课程\",\n" +
                "                \"state\": 0,\n" +
                "                \"resource_count\": 0,\n" +
                "                \"created_at\": \"2023-01-04 14:49:52\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 13063385,\n" +
                "                \"name\": \"最新课程\",\n" +
                "                \"alias\": \"最新课程\",\n" +
                "                \"state\": 0,\n" +
                "                \"resource_count\": 0,\n" +
                "                \"created_at\": \"2023-01-04 14:49:45\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 13063384,\n" +
                "                \"name\": \"人气课程\",\n" +
                "                \"alias\": \"人气课程\",\n" +
                "                \"state\": 0,\n" +
                "                \"resource_count\": 0,\n" +
                "                \"created_at\": \"2023-01-04 14:49:38\"\n" +
                "            }\n" +
                "        ]";
        mapList = JSON.parseArray(DEMO_STR, Map.class);

        List<ResTagPO> tagList = mapList.stream().map(o -> {

//            alias	string	无
//            app_id	string	店铺id
//            id	int	商品分组id
//            name	string	商品分组名称
//            order_weight	int	排序位置，自增类型，默认组件返回0开始，0为靠前位置，例如0 1 2 3 4 5
//            rank	int	商品在商品分组中的序号
//            resource_count	int	商品分组总数
//            state	int	状态(0-可见 1-隐藏)
//            type	int	类型：1-图文、2-音频、3-视频、4-直播、5-会员、6-专栏、7-社群、8-打卡、9-活动、10-问答、11-频道、12-精选、13-最新、14-最热、15-评测
//            created_at	string	创建时间
//            updated_at	string	更新时间


            ResTagPO po = new ResTagPO();
            po.setGroupId(Long.parseLong("" + o.get("id")));
            po.setTitle("" + o.get("name"));
            po.setGoodsCount(Integer.parseInt("" + o.get("resource_count")));
            po.setAlias("" + o.get("alias"));

            return po;
        }).collect(Collectors.toList());

//        ImmutableMap ret = ImmutableMap.of("total", data.get("total"), "content", tagList);

        return tagList;
    }

    public Map getTagResourceList(int pageNum, int pageSize, Long groupId) {
        String accessToken = ""; // tokenService.accessToken();
        //请求参数
        //access_token	是	string	专属token
        //tag_id	是	string	商品分组id
        //page	是	int	页码
        //page_size	是	int	每页数据量，最大限制100
        Map params = ImmutableMap.of(
                "access_token", accessToken,
                "page", pageNum,
                "page_size", pageSize,
                "tag_id", "" + groupId);


//        Map result = (Map) xetRequest(XET_API_TAG_RESOURCE_LIST_AP, params);


//        Map resources = (Map) result.get("resources");
        //current_page	int	当前页数
        //total	int	视频分组总数
        //last_page	int	最后页数
        //per_page	int	每页商品分组数
        //from	int	返回结果在搜索结果的开始索引
        //to	int	返回结果在搜索结果的结束索引
        //data	array	商品列表数据
        List<Map> data = new ArrayList<>(); //(List<Map>) resources.get("data");

        List<GoodsListPO> liveList = data.stream().map(o -> {
//id	int	自增主键
//app_id	string	店铺ID
//resource_id	string	资源id
//spu_id	string	统一商品spu_id
//tag_id	int	商品分组id
//title	int	商品title
//summary	string	资源简介
//img_url	string	封面图片url
//img_url_compressed_larger	string	压缩列表配图url-大图
//img_url_compressed	string	压缩列表配图url
//alive_img_url	string	首页展示的直播图
//payment_type	int	付费类型：1-免费、2-单笔、3-付费产品包、4-团购、5-单笔的购买赠送、6-产品包的购买赠送、7-问答提问、8-问答偷听、9-购买会员、10-会员的购买赠送、11、活动付费报名 12、打赏 13、拼团单个资源 14、拼团产品包 15、超级会员 16-接入商兑换码
//price	int	商品价格
//line_price	int	划线价
//start_at	string	上架/开始时间
//stop_at	string	下架/结束时间
//state	int	状态 0-可见 1-隐藏 2-删除
//recycle_state	int	上下架状态 0-上架，1-下架
//period	int	有效期 单位：秒
//sell_mode	int	销售类型 0-自营商品，1-内容市场商品
//product_name	string	产品包名
//view_count	int	视频浏览量次数
//comment_count	int	评论次数
//resource_count	int	资源数
//feeds_count	int	圈子动态数量
//order_weight	int	权重
//finished_state	int	专栏完结状态 0-未完结，1-已完结
//type	int	资源类型 资源类型：1-图文、2-音频、3-视频、4-直播、5-会员、6-专栏、8-大专栏、12-分销商品、25-训练营、29-线下课、33-采购课、45-AI互动课
//content_app_id	string	内容方appid
//resource_created	string	商品创建时间
//zb_start_at	string	直播开始时间
//zb_stop_at	string	直播结束时间
//manual_stop_at	string	主播手动结束直播时间
//is_stop_sell	int	是否停售 0-否、1-是
//is_transcode	int	商品如果为视频是否转码 , 0-未转码、1-已转码
//act_type	int	打卡活动类型 0-旧课程打卡 1-日历打卡 2-作业打卡 3-闯关打卡
//created_at	string	资源创建时间
//updated_at	string	资源更新时间
//alive_type	int	直播类型 0-语音直播,1-视频直播,2-推流直播,3-ppt直播,4-分享直播
//sub_type	int	商品类型type的扩展类型
            GoodsListPO po = new GoodsListPO();
            if (Integer.parseInt("" +o.get("recycle_state")) == 1){
                return po;
            }
//            po.setSellStatus(Integer.parseInt("" + o.get("sale_status")));

            po.setImageUrl("" + o.get("img_url"));

            po.setItemId(Integer.parseInt("" + o.get("id")));
            po.setTitle("" + o.get("title"));
            po.setType(1); //商品类型枚举值，1：专栏； 2：内容； 4：直播 ；10：课程

            String resId = "" + o.get("resource_id");
            int type = Integer.parseInt("" + o.get("type"));
            po.setAlias(genAlias(type, resId));
            po.setUrl(po.getAlias());

            po.setMediaType(type); //内容的类型，1:图文内容2:音频内容3:视频内容

            po.setSellerType(3);
            po.setSellStatus(0);

            po.setCreatedTime(DateUtil.parseToDateMilis(String.valueOf(o.get("created_at"))).getTime());
            po.setLiveStartAt(DateUtil.parseToDateMilis(String.valueOf(o.get("created_at"))).getTime());
            return po;
        }).collect(Collectors.toList());

        ImmutableMap ret = ImmutableMap.of("total", data.size() /*resources.get("total")*/, "content", liveList);

        return ret;
    }

    private String genAlias(int type, String resourceId){

//        图文：https://{appid}.h5.xiaoeknow.com/v1/course/text/{resource_id}?type=2
//        音频：https://{appid}.h5.xiaoeknow.com/v1/course/audio/{resource_id}?type=2
//        视频：https://{appid}.h5.xiaoeknow.com/v1/course/video/{resource_id}?type=2
//        直播：https://{appid}.h5.xiaoeknow.com/v2/course/alive/{resource_id}?type=2
//        专栏：https://{appid}.h5.xiaoeknow.com/v1/course/column/{resource_id}?type=3
//        会员：https://{appid}.h5.xiaoeknow.com/v1/course/member/{resource_id}?type=3
//        电子书|AI互动课|训练营营期|班课|线下课：https://{appid}.h5.xiaoeknow.com/v1/goods/goods_detail/{resource_id}?type=2
        String [] formats = {
                "",
                "https://%s.h5.xiaoeknow.com/v1/course/text/%s?type=2", //图文
                "https://%s.h5.xiaoeknow.com/v1/course/audio/%s?type=2", //音频
                "https://%s.h5.xiaoeknow.com/v1/course/video/%s?type=2",  //视频
                "https://%s.h5.xiaoeknow.com/v2/course/alive/%s?type=2",  //直播
                "https://%s.h5.xiaoeknow.com/v1/course/member/%s?type=3",  //会员
                "https://%s.h5.xiaoeknow.com/v1/course/column/%s?type=3",  //专栏
                ""
        };

        if (type < 1 || type > 6){
//        电子书|AI互动课|训练营营期|班课|线下课：https://{appid}.h5.xiaoeknow.com/v1/goods/goods_detail/{resource_id}?type=2
            return null;
        }

        String format = formats[type];
        String alias = String.format(format, appId, resourceId);
        return alias;
    }
}
